// const axios = require('axios')
// const Dysmsapi20170525  = require('@alicloud/dysmsapi20170525').default;
// const {Config}=require('@alicloud/openapi-client')
const tencentcloud=require('tencentcloud-sdk-nodejs')
const SmsClient=tencentcloud.sms.v20210111.Client

const client=new SmsClient({
    credential:{
        secretId:process.env.TENCENT_SECRET_ID,
        secretKey:process.env.TENCENT_SECRET_KEY
    },
    region:'ap-guangzhou'
})


function generateCode(){
    return Math.floor(10000+Math.random()*900000).toString()
}

async function sendSMS(phone) {
    const code=generateCode()
    try{
        await client.sendSms({
            PhoneNumbers:[`+86${phone}`],
            SmsSdkAppId:process.env.SMS_SDK_APP_ID,
            SignName:process.env.SMS_SIGN_NAME,
            TemplateId:process.env.SMS_TEMPLATE_ID,
            TemplateParamSet:[code],
        })
        return {
            success:true,
            code,
            expiresAt:new Date(Date.now()+5*60*1000)
        }
    }catch(err){
        console.log('短信发送失败',err);
        return{success:false}
    }
}

module.exports={sendSMS}


